/* ***************************************************************
//  CMemcached.h   version:  1.0   date: 03/26/2011
//  -------------------------------------------------------------
//  Author: Wang Haoran
//  Email: cwhr_x@yahoo.com.cn
//  -------------------------------------------------------------
//  Copyright (C) 2011 HongYue - All Rights Reserved 
***************************************************************/

#ifndef __CMEMCACHED_H__
#define __CMEMCACHED_H__


#include <iostream>

#include <boost/archive/text_oarchive.hpp> 
#include <boost/archive/text_iarchive.hpp> 
#include "comm_exception.h"
using namespace C2217::StdLib;
enum {CMS_TYPE_SAVE = 1, CMS_TYPE_LOAD};

extern "C"{

#include <libmemcached/memcached.h>
};

class CMemStream {

public:
	CMemStream(int iType = CMS_TYPE_SAVE);
	void SetKey(char *szKey);
	const char* GetKey();
	void SetVal(std::stringstream ssm);
	void SetVal(char * p);
	std::stringstream & GetVal();
	int GetValLen();
	void RecvVal(char*);
	void Clear();

public:
	boost::archive::text_oarchive * mp_oa;
	boost::archive::text_iarchive * mp_ia;
	
private:
	std::stringstream m_ss;
	std::string m_strKey;
};

class CMemcached {
public:
	CMemcached(char* szSvrName, unsigned int iSvrPost, memcached_behavior be = MEMCACHED_BEHAVIOR_TCP_NODELAY);
	~CMemcached();
	
	memcached_return SetBehavior(memcached_behavior be, uint64_t val);
	
	void SetKeyVal(char* KEY, char * val);
	void SetKeyVal(char* KEY, char * val, int iLen);
	char * GetKeyVal(char *KEY);
	void SetKeyVal(CMemStream& cmstream);
	void GetKeyVal(CMemStream& cmstream);
	uint64_t IncrementKeyVal(char* KEY);
	void DeleteKey(char* KEY);
	memcached_return FlushAll();
	char* GetErrString();
	bool IsKeyExist(char *KEY);

private:
	memcached_st *m_memc;
	memcached_return m_rc;
};

//void SetKeyVal(IStream& stream);
//void GetKeyVal(IStream& stream);
//
//CStream stream;
//stream.SetKey("test");
//memcache.GetKeyVal(stream);

//class CMemcachedException {
//public:
//};

#endif //CMEMCACHED_H__DEF
